package model.building;

import model.Ressources;
import model.City;
import model.util.Vector3D;
import model.util.Object3D;
import model.util.Box3D;

public abstract class Company extends Building {
    
    /** Constructeurs */
    public Company(City city, Object3D object3d, Activity activity) {
        super(city, object3d, activity);
    }

    /** Classes internes */
    public static abstract class Activity extends Building.Activity {

        /** Attributs */
        private Ressources m_investment;
        private Ressources m_toUse, m_toCreate;

        /** Constructeurs */
        public Activity(Activity activity) { 
            m_toUse = new Ressources(activity.m_toUse);
            m_toCreate = new Ressources(activity.m_toCreate);
            m_investment = new Ressources(activity.m_investment);
        }

        public Activity() { 
            m_toUse = new Ressources();
            m_toCreate = new Ressources();
            m_investment = new Ressources();
        }
        
        /** Accesseurs */
        public Ressources getInvestment() { return m_investment; }

        public Ressources getImpact(double deltaTps, Ressources economy) {
            return getImpact(deltaTps, economy, new Ressources()); 
        }

        /** Methodes */
        protected void initRessourceToUse(Ressources.Type type, int quantity) {
            m_toUse.add(type, quantity); 
            m_investment.set(type, 0);
        }
        protected void initRessourceToCreate(Ressources.Type type, int quantity) { m_toCreate.add(type, quantity); } 
	
        public void invest(Ressources.Type type, int quantity, Ressources economy) {
            economy.transferTo(getInvestment(), type, quantity); 
        }

        public void construct(Ressources economy) {}
        public Ressources getImpact(double deltaTps, Ressources economy, Ressources invest) {
            Ressources ret = new Ressources();
            
            double ratioMin = 1;
            for (Ressources.Type key : Ressources.Type.values()) 
                if (m_toUse.isAvailable(key)) 
                    ratioMin = Math.min(ratioMin, invest.getQuantity(key) / m_toUse.getQuantity(key));

            for (Ressources.Type key : Ressources.Type.values()) {
                if (m_toUse.isAvailable(key)) invest.remove(key, m_toUse.getQuantity(key) * ratioMin);
                if (m_toCreate.isAvailable(key)) ret.add(key, m_toCreate.getQuantity(key) * ratioMin);
            }

            return ret;
        }
        public void destroy(Ressources economy) {}

        public void update(double deltaTps, Ressources economy) { 
            applyImpact(getImpact(deltaTps, economy, economy), economy); 
        }

    }
}
